import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueDevTools from 'vite-plugin-vue-devtools'

import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

// https://vite.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    vueDevTools(),
    AutoImport({
      resolvers: [ElementPlusResolver()],
    }),
    Components({
      resolvers: [
        ElementPlusResolver({
          // 启用暗黑模式
          importStyle: 'css',
        }),
      ],
    }),
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url)),
    },
  },
  css: {
    preprocessorOptions: {
      scss: {
        // additionalData: `@use "@/assets/index.scss" as *;`,
      },
    },
  },
  server: {
    proxy: {
      // 凡是 /api/qweather 开头的请求都代理到 https://mw4ewr36cv.re.qweatherapi.com
      '/api/qweather': {
        target: 'https://mw4ewr36cv.re.qweatherapi.com',
        changeOrigin: true, // 修改请求头里的 Host
        rewrite: (path) => path.replace(/^\/api\/qweather/, ''), // 去掉前缀
      },
      '/ws/location/v1/ip': {
        target: 'https://apis.map.qq.com',
        changeOrigin: true,
        // rewrite: (path) => path.replace(/^\/ws\/location\/v1\/ip/, ''),
      },
    },
  },
})
